package com.cs.service.carousel;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cs.mapper.CarouselMapper;
import com.cs.pojo.CarouselMap;
import com.cs.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ICarouselServiceImpl extends ServiceImpl<CarouselMapper, CarouselMap> implements ICarouselService{
    @Autowired
    private CarouselMapper carouselMapper;

    @Override
    public IPage<CarouselMap> getPage(int currentPage, int pageSize) {
        LambdaQueryWrapper<CarouselMap> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage<CarouselMap> page=new Page<>(currentPage,pageSize);
        carouselMapper.selectPage(page,lambdaQueryWrapper);
        return page;
    }

    @Override
    public List<CarouselMap> getByTopic(String topic) {
        LambdaQueryWrapper<CarouselMap> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CarouselMap::getTopic,topic);
        List<CarouselMap> carouselMapList = carouselMapper.selectList(lambdaQueryWrapper);
        return carouselMapList;
    }

    @Override
    public IPage<CarouselMap> getPageByTopic(int currentPage, int pageSize, String topic) {
        LambdaQueryWrapper<CarouselMap> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(CarouselMap::getTopic,topic);
        IPage<CarouselMap> page=new Page<>(currentPage,pageSize);
        carouselMapper.selectPage(page,lambdaQueryWrapper);
        return page;
    }

    @Override
    public CarouselMap getInUsing() {
        LambdaQueryWrapper<CarouselMap> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CarouselMap::getIsUsing,1);
        CarouselMap carouselMap = carouselMapper.selectOne(lambdaQueryWrapper);
        return carouselMap;
    }
    @Override
    public Boolean setUsingCarousel(Integer id){
        CarouselMap inUsing = getInUsing();
        if(inUsing!=null){
            inUsing.setIsUsing(0);
            carouselMapper.updateById(inUsing);
        }
        CarouselMap carouselMap = carouselMapper.selectById(id);
        carouselMap.setIsUsing(1);
        int i = carouselMapper.updateById(carouselMap);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

}
